﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace SolutionGenerator
{
    public partial class WizardGetTemplates : UserControlParent
    {
        private enum ColumnIndex
        {
            FolderLocation = 0,
            TemplateFile,
            DataTypeFile,
            FormatName,
            DataIndex
        }
        public WizardGetTemplates(SolutionConfiguration configuration) : base(configuration)
        {
            InitializeComponent();
        }
        public override bool IsPageValid
        {
            get
            {
                return ConfigurationFolder.Count > 0;
            }
        }
        public override string ValidationMessage
        {
            get
            {
                return TextMessage.WizardValidation_GetTemplate;
            }
        }
        public override void Save()
        {
            for (int i = 0; i < SolutionGeneratorGetTemplateDataGridView.Rows.Count; i++)
            {
                string nameFormat = (string)SolutionGeneratorGetTemplateDataGridView.Rows[i].Cells[(int)ColumnIndex.FormatName].Value;
                UpdateFormatName(i, nameFormat);
            }
            GetSolutionConfiguration.GetFolderInfo = ConfigurationFolder;
        }
        public void BindDataGridView()
        {
            SolutionGeneratorGetTemplateDataGridView.Rows.Clear();
            for(int i =0;i<ConfigurationFolder.Count;i++)
            {
                SolutionConfigurationFolderInfo conf = ConfigurationFolder[i];
                int newRow = SolutionGeneratorGetTemplateDataGridView.Rows.Add();
                DataGridViewRow row = SolutionGeneratorGetTemplateDataGridView.Rows[newRow];
                DirectoryInfo folderLoc = new DirectoryInfo(conf.FolderLocation);
                row.Cells[(int)ColumnIndex.FolderLocation].Value = folderLoc.Name;
                string value = string.Empty;
                if (!string.IsNullOrEmpty(conf.UseThisTemplate))
                {
                    FileInfo templateFile = new FileInfo(conf.UseThisTemplate);
                    if (templateFile.Exists)
                        value = templateFile.Name;
                    else
                        value = "Browse";
                }
                else
                    value = "Browse";
                row.Cells[(int)ColumnIndex.TemplateFile].Value = value;
                if (!string.IsNullOrEmpty(conf.DataTypeTemplate))
                {
                    FileInfo dataTypeFile = new FileInfo(conf.DataTypeTemplate);
                    if (dataTypeFile.Exists)
                        value = dataTypeFile.Name;
                    else
                        value = "Browse";
                }
                else
                    value = "Browse";
                row.Cells[(int)ColumnIndex.DataTypeFile].Value = value;
                row.Cells[(int)ColumnIndex.DataIndex].Value = i;
                row.Cells[(int)ColumnIndex.FormatName].Value = conf.NameFormat;
            }
        }
        private List<SolutionConfigurationFolderInfo> ConfigurationFolder;
        public override void LoadWizard()
        {
            ConfigurationFolder = GetSolutionConfiguration.GetFolderInfo ?? new List<SolutionConfigurationFolderInfo>();
            BindDataGridView();
        }
        public override string WizardTitle
        {
            get
            {
                return TextMessage.WizardTitle_GetTemplate;
            }
        }
        private void UpdateDataTypeFile(int rowIndex, string newDataTypeFile)
        {
            SolutionConfigurationFolderInfo target = ConfigurationFolder[rowIndex];
            if (target != null)
                target.DataTypeTemplate = newDataTypeFile;
        }
        private void UpdateTemplateFile(int rowIndex, string newTemplateFile)
        {
            SolutionConfigurationFolderInfo target = ConfigurationFolder[rowIndex];
            if (target != null)
                target.UseThisTemplate = newTemplateFile;
        }
        private void UpdateFolderTemplate(int rowIndex, string newTemplateFolder)
        {
            SolutionConfigurationFolderInfo target = ConfigurationFolder[rowIndex];
            if (target != null)
                target.FolderLocation = newTemplateFolder;
        }
        private void UpdateFormatName(int rowIndex, string formatName)
        {
            SolutionConfigurationFolderInfo target = ConfigurationFolder[rowIndex];
            if (target != null)
                target.NameFormat = formatName;
        }
        private void AddFolderTemplate(string templateFolder)
        {
            ConfigurationFolder.Add(new SolutionConfigurationFolderInfo()
            {
                FolderLocation = templateFolder
            });
        }
        private void SolutionGeneratorGetTemplateGetFolderButton_Click(object sender, EventArgs e)
        {
            if (SolutionGeneratorGetTemplateOpenDirDialog.ShowDialog() == DialogResult.OK)
            {
                AddFolderTemplate(SolutionGeneratorGetTemplateOpenDirDialog.SelectedPath);
                BindDataGridView();
            }
        }
        private void SolutionGeneratorGetTemplateDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridView grid = (DataGridView)sender;
                int rowIndex = (int)grid.Rows[e.RowIndex].Cells[(int)ColumnIndex.DataIndex].Value;
                switch (e.ColumnIndex)
                {
                    case (int)ColumnIndex.FolderLocation:
                        SolutionGeneratorGetTemplateOpenDirDialog.SelectedPath = ConfigurationFolder[rowIndex].FolderLocation;
                        if (SolutionGeneratorGetTemplateOpenDirDialog.ShowDialog() == DialogResult.OK)
                        {
                            UpdateFolderTemplate(rowIndex, SolutionGeneratorGetTemplateOpenDirDialog.SelectedPath);
                            BindDataGridView();
                        }
                        break;
                    case (int)ColumnIndex.TemplateFile:
                        if (SolutionGeneratorGetTemplateOpenFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            UpdateTemplateFile(rowIndex, SolutionGeneratorGetTemplateOpenFileDialog.FileName);
                            BindDataGridView();
                        }
                        break;
                    case (int)ColumnIndex.DataTypeFile:
                        if (SolutionGeneratorGetTemplateOpenFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            UpdateDataTypeFile(rowIndex, SolutionGeneratorGetTemplateOpenFileDialog.FileName);
                            BindDataGridView();
                        }
                        break;
                }
            }
        }
        private void SolutionGeneratorGetTemplateDataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            ConfigurationFolder.RemoveAt((int)e.Row.Cells[(int)ColumnIndex.DataIndex].Value);
            BindDataGridView();
        }
    }
}